package edu.gyc.hishiro.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import edu.gyc.hishiro.dao.SysRoleDao;
import edu.gyc.hishiro.model.SysRole;
import edu.gyc.hishiro.model.SysUserRole;
import edu.gyc.hishiro.model.UserInfo;
import edu.gyc.hishiro.service.SysRoleService;
import edu.gyc.hishiro.service.SysUserRoleService;
import edu.gyc.hishiro.service.UserInfoService;
import edu.gyc.hishiro.vo.RoleUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ls
 * @since 2021-01-04
 */
@Controller
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    SysRoleService roleService;


    @RequestMapping("/query")
    public String query(Model model) {

        model.addAttribute("roleUsers", roleService.getRoleUserList());
        return "/admin/perms";
    }

    @RequestMapping("/user")
    public String queryUser(Model model) {
        List<SysRole> roles = roleService.list();
        model.addAttribute("roles", roles);
        return "/admin/perms";
    }

    @RequestMapping("/queryperms")
    public String queryperms(Integer id, Model model) {


        model.addAttribute("myperms", roleService.queryPermissionByRoleId(id));
        model.addAttribute("roleId", id);
        model.addAttribute("roleName", roleService.lambdaQuery().eq(SysRole::getId, id).one().getDescription());
        return "/admin/myperms";
    }

    @RequestMapping("/updateUser")
    public String updateUser(Integer id, Model model) {
        Set<RoleUser> roleUsers = roleService.getRoleUserList();
        List<String> userNameList = new ArrayList<>();
        for (RoleUser roleUser : roleUsers) {
            if (roleUser.getId() == id) {
                for (String name : roleUser.getNameSet()) {
                    userNameList.add(name);
                }
                model.addAttribute("roleUser", roleUser);
                model.addAttribute("names", userNameList);
            }
        }

        model.addAttribute("allroleuser", roleService.getAllRoleUserList());

        return "/admin/role";
    }

    @PostMapping("/updateRolePermissions")
    public String form1(String[] acquireId, Integer roleId,Model model)  {
        String a = Arrays.toString(acquireId);
        List<Integer> ids = new ArrayList<>();
        if (acquireId.length > 0) {
            for (String id : acquireId) {
                ids.add(Integer.parseInt(id));
            }
        }

        roleService.updateRolePermissions(roleId, ids);


        model.addAttribute("qxmsg","角色权限更新成功！");
        model.addAttribute("roleId",roleId);

        return "/admin/permmsg";
    }

    @Autowired
    SysUserRoleService userRoleService;

    @PostMapping("/addRoleUser")
    public String form2(Integer uid, Integer roleId, Model model) {
        userRoleService.addRoleUser(uid, roleId);

        return "redirect:/role/query";
    }

    @Autowired
    UserInfoService userInfoService;
    @RequestMapping("/delUser")
    public String delUser(Integer roleId,String username) {
        UserInfo userInfo=userInfoService.lambdaQuery().eq(UserInfo::getUsername,username).one();
        if (userInfo != null) {
            userRoleService.remove(new QueryWrapper<SysUserRole>().lambda()
                    .eq(SysUserRole::getUid,userInfo.getUid()).eq(SysUserRole::getRoleId,roleId));
        }

        return "redirect:/role/query";
    }
}

